import __builtin__

from client.unitsystem.unit import Unit
from server.sessionstuff.webentities import webunit
from panda3d.core import NodePath
from direct.actor.Actor import Actor


def get_unit_data(name):
    if name == "Drillbot":
        return UnitData("Drillbot", 3.5, 0.4, 300, 3, [], 8, "Drill")
    if name == "Knockknockbot":
        return UnitData("Knockknockbot", 3.5, 0.4, 200, 2, ["Drillbot", "Minitank"], 5, "Klopfer")
    if name == "Minitank":
        return UnitData("Minitank", 2.5, 0.3, 300, 5, [], 7, "Gun")
    if name == "warpzone":
        return UnitData("warpzone", 4, 0.25, 0, 0, [], 100, "")
            #UnitData(name, speed, radius, hp, worth, upgrades[], cost)


def create_client_unit(name):
    data = get_unit_data(name)
    unit = Unit(_get_model(name), data.speed, data.name, data.radius, data.hp, data.worth, data.upgrades, data.cost)
    if name == "Pandaling":
        unit.unit_object.setScale(0.4, 0.4, 0.4)
    if name == "FatPanda":
        unit.unit_object.setScale(1.4, 1.4, 1.4)
    return unit


def create_web_unit(name):
    data = get_unit_data(name)
    unit = webunit.WebUnit(data.name, data.speed, data.radius, data.hp, data.worth, data.upgrades, data.cost, data.weapon)
    return unit


def _get_model(name):
    #unit_object = NodePath("Model")
    #unit_object.setScale(0.002, 0.002, 0.002)
    model = 0
    if name == "Pandaling" or name == "TestUnit":
        model = Actor("models/panda-model",
                                {"walk": "models/panda-walk4"})
        model.loop("walk")
        model.setScale(0.0015, 0.0015, 0.0015)
    if name == "Minitank":
        model = __builtin__.loader.loadModel("client/models/mech")
        #model = Actor("client/models/navi", {"land": "client/models/navi_land"})
        #model.loop("land")
        model.setScale(0.5, 0.5, 0.5)
        #model.setH(-180)
    if name == "Knockknockbot":
        model = Actor("client/models/anklopfer", {"move": "client/models/anklopfer_move", "attack": "client/models/anklopfer_attack"})
        model.setScale(0.4, 0.4, 0.4)
        model.loop("move")
    if name == "Drillbot":
        model = Actor("client/models/drill", {"move": "client/models/drill_move", "attack": "client/models/drill_attack"})
        model.loop("move")
        model.setScale(0.4, 0.4, 0.4)
    if name == "warpzone":
        model = Actor("client/models/navi", {"land": "client/models/navi_land"})
        model.setScale(0.3, 0.3, 0.3)
        #model = __builtin__.loader.loadModel("client/models/mech")
    #model.reparent_to(unit_object)
    return model


def get_weapon(name):
    if name == "Klopfer":
        return WeaponData(name, 15, 1.0, 1, True, "Klopfer")
    if name == "Drill":
        return WeaponData(name, 7, 0.25, 1, True, "Drill")
    if name == "Gun":
        return WeaponData(name, 50, 2, 3, False, "Bullet")


class UnitData():

    def __init__(self, name, speed, radius, hp, worth, upgrades, cost, weapon):
        self.name = name
        self.speed = speed
        self.radius = radius
        self.hp = hp
        self.worth = worth
        self.upgrades = upgrades
        self.cost = cost
        self.weapon = weapon


class WeaponData():

    def __init__(self, name, damage, atkrate, range, direct, animation):
        self.name = name
        self.damage = damage
        self.atkrate = atkrate
        self.range = range
        self.animation = animation
        self.direct = direct
        self.cooldown = 0